﻿// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using Microsoft.Kinect;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Media;

namespace Coding4Fun.Chair.Kinect
{
	public class Sensor
	{
		KinectSensor _sensor;

		public event EventHandler<Microsoft.Kinect.AllFramesReadyEventArgs> AllFramesReady;


		public Sensor()
		{
			SetupKinect(); 
		}

		private void SetupKinect()
		{
			if (KinectSensor.KinectSensors.Count > 0)
			{
				//pull the first Kinect
				_sensor = KinectSensor.KinectSensors[0];
			}

			if (_sensor.Status != KinectStatus.Connected || KinectSensor.KinectSensors.Count == 0)
			{
				MessageBox.Show("No Kinect connected");
				return;
			}

			_sensor.AllFramesReady += new EventHandler<AllFramesReadyEventArgs>(_sensor_AllFramesReady);
			_sensor.SkeletonStream.Enable(); 
			_sensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
			_sensor.DepthStream.Enable(DepthImageFormat.Resolution320x240Fps30);


		}

		public void Start()
		{
			_sensor.Start();
		}

		void _sensor_AllFramesReady(object sender, AllFramesReadyEventArgs e)
		{
			AllFramesReady(sender, e); 
		}

		public void Stop()
		{
			_sensor.Stop(); 
		}



		//public void Open()
		//{
		//    if (_isInit)
		//        Close();

			

		//    if (TrackSkeleton)
		//    {
		//        flags |= RuntimeOptions.UseDepthAndPlayerIndex;
		//        flags |= RuntimeOptions.UseSkeletalTracking;
		//    }
		//    else if (UseDepthCameraStream)
		//    {
		//        flags |= RuntimeOptions.UseDepth;
		//    }

		//    if (UseColorCameraStream)
		//    {
		//        flags |= RuntimeOptions.UseColor;
		//    }

		//    _runtime.Initialize(flags);

		//    if (TrackSkeleton || UseDepthCameraStream)
		//    {
		//        var imageType = (TrackSkeleton) ? ImageType.DepthAndPlayerIndex : ImageType.Depth;
		//        _runtime.DepthStream.Open(ImageStreamType.Depth, 2, DepthResolution, imageType);
		//    }

		//    // now open streams
		//    if (UseColorCameraStream)
		//    {
		//        _runtime.VideoStream.Open(ImageStreamType.Video, 2, ImageResolution.Resolution640x480, ImageType.Color);
		//    }


		//    _runtime.VideoFrameReady += RuntimeColorFrameReady;
		//    _runtime.DepthFrameReady += RuntimeDepthFrameReady; 
		//    _runtime.SkeletonFrameReady += RuntimeSkeletonFrameReady;
			
		//    _isInit = true;
		//}


	}
}
